package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.entity.evaluate.EvaluateVO;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.rest.evaluate.FindEvaluate;
import com.burgeon.backend.SHOPBACKEND.rest.evaluate.ModEvaluateState;
import com.burgeon.backend.SHOPBACKEND.rest.evaluate.QuerryEvaluateData;
import com.burgeon.backend.SHOPBACKEND.service.BackendEvaluateService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.mappers.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class BackendEvaluateServiceImpl implements BackendEvaluateService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    /**
     * 条件搜索
     * @param field A 会员卡号 B线上单号 C云仓单号
     * @param message
     * @param account
     * @param start
     * @return
     */
    @Override
    public HashMap FindData(String field, String message, String account,Integer start) {
        HashMap<String,String> condition = new HashMap<String,String>();
        condition.put("A","C_VIP_ID;CARDNO");
        condition.put("B","YC_RETAIL_ID;XSNO");
        condition.put("C","YC_RETAIL_ID;DOCNO");

        Info info = GetInfo(account);
        FindEvaluate findEvaluate = new FindEvaluate(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap map = new HashMap();

        ArrayList<EvaluateVO> data = null;
        try {
            data = findEvaluate.getData(condition.get(field),message,start);
            map.put("count", data.get(0).getCount());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        for (EvaluateVO evaluateVO : data) {

            try {
                //款号获取商品
                //uid获取会员
                Goods goods = backendGoodsMapper.findGoodsDataByCode(evaluateVO.getGoodsCode(), account);
                if (null != goods) {
                    evaluateVO.setGoodsImg(goods.getImg());
                    evaluateVO.setGoodsPrice(goods.getPrice());
                    evaluateVO.setGoodsName(goods.getName());
                }

                User user = userMapper.FindUserDataByPhone(evaluateVO.getVipPhone(), account);
                if (null != user) {
                    evaluateVO.setVipImg(user.getImg());
                }
                evaluateVO.setCount(null);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        map.put("data", data);


        return map;
    }

    @Override
    public void showHidden(String id,String state, String account) throws ErrorDataException {
        Info info = GetInfo(account);
        ModEvaluateState modEvaluateState = new ModEvaluateState(info.getUrl(), info.getAccount(), info.getPassword());
        try {
            modEvaluateState.getData(id,state);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
    }

    @Override
    public HashMap getData(String account, Integer start) {
        Info info = GetInfo(account);
        QuerryEvaluateData querryEvaluateData = new QuerryEvaluateData(info.getUrl(), info.getAccount(), info.getPassword());
        HashMap map = new HashMap();

        ArrayList<EvaluateVO> data = null;
        try {
            data = querryEvaluateData.getData(start);
            map.put("count", data.get(0).getCount());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        for (EvaluateVO evaluateVO : data) {

            try {
                //款号获取商品
                //uid获取会员
                Goods goods = backendGoodsMapper.findGoodsDataByCode(evaluateVO.getGoodsCode(), account);
                if (null != goods) {
                    evaluateVO.setGoodsImg(goods.getImg());
                    evaluateVO.setGoodsPrice(goods.getPrice());
                    evaluateVO.setGoodsName(goods.getName());
                }

                User user = userMapper.FindUserDataByPhone(evaluateVO.getVipPhone(), account);
                if (null != user) {
                    evaluateVO.setVipImg(user.getImg());
                }
                evaluateVO.setCount(null);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        map.put("data", data);

        return map;

    }

    private Info GetInfo(String token) {
        return accountMapper.GetAccData(token);
    }
}
